package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.server.vo.PageParams;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Autowired
    private TodayBestService todayBestService;

    /*
     *  今日佳人功能
     * */
    public TodayBest todayBest(String token) {
        //1.解析token
        User user = userService.queryToekn(token);
        //2.根据user id 查询mongo中推荐的佳人
        RecommendUser recommendUser = recommendService.findByUid(user.getId());
        if (recommendUser == null) {
            //没有佳人 设置一个默认佳人
            recommendUser = new RecommendUser();
            recommendUser.setUserId(defaultUser);
            recommendUser.setScore(88D);
        }
        //3.调用封装的getTodayBest方法 将推荐好友实体类装换成TodayBest对象返回
        TodayBest todayBest = todayBestService.getTodayBest(recommendUser);
        return todayBest;
    }

    /*
     * 查询家人推荐列表
     * */
    public PageResult recommendation(String token, PageParams pageParams) {
        //1.解析token
        User user = userService.queryToekn(token);
        //2.根据id查询所有的推荐好友
        List<RecommendUser> list = recommendService.findAllByUid(user.getId(), pageParams.getPage(), pageParams.getPagesize());

        //3.定义一个集合用来存放TodayBest对象
        List<TodayBest> todayBestList = new ArrayList<>();

        //4.遍历集合 将推荐好友实体类装换成TodayBest对象 添加到todayBestList中
        for (RecommendUser recommendUser : list) {
            TodayBest todayBest = todayBestService.getTodayBest(recommendUser);
            todayBestList.add(todayBest);
        }

        //5.创建PageResult对象 封装需要返回前端的数据
        PageResult result = new PageResult();
        result.setPage(pageParams.getPage());
        result.setPagesize(pageParams.getPagesize());
        result.setItems(todayBestList);

        //6.返回PageResult对象
        return result;
    }

    /*
     * 根据推荐好友实体类RecommendUser对象封装成TodayBest对象
     * */
    public TodayBest getTodayBest(RecommendUser recommendUser) {
        //1.userId个人信息
        LambdaUpdateWrapper<UserInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserInfo::getUserId, recommendUser.getUserId());
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        //2.封装页面需要的数据结果
        TodayBest todayBest = new TodayBest();
        todayBest.setFateValue(Double.valueOf(Math.floor(recommendUser.getScore())).longValue());
        //3.封装个人信息
        todayBest.setId(userInfo.getUserId());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setAge(userInfo.getAge());
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        //4.返回封装的TodayBest对象
        return todayBest;
    }
}
